<?php

class Kd_Html_Pagination extends Kd_Html_Element {

    private $_informationTextFormat = '';
    private $_urlFormat = '';
    private $_pageSize = 20;
    private $_pageIndex = 1;
    private $_recordCount = 0;
    private $_pageRange = 10;
    private $_startPage = NULL;
    private $_endPage = NULL;
    private $_pageCount = NULL;
    private $_pageSizes = array();
    private $_pageIndexTextFormat = '';
    private $_pageSizeTextFormat = '';
    private $_firstPageText = '';
    private $_nextPageText = '';
    private $_prevPageText = '';
    private $_lastPageText = '';
    private $_pageSizeText = '';

    public function __construct(){
        parent::__construct();
        $this->templateName = 'pagination';
        $this->addClass('pagination');
    }

    public function getUrlFormat(){
        return $this->_urlFormat;
    }

    public function setUrlFormat($value){
        $this->_urlFormat = $value;
        return $this;
    }

    public function getPageSize(){
        return $this->_pageSize;
    }

    public function setPageSize($value){
        $this->_pageSize = $value;
        return $this;
    }

    public function getPageIndex(){
        return $this->_pageIndex;
    }

    public function setPageIndex($value){
        $this->_pageIndex = $value;
        return $this;
    }

    public function getRecordCount(){
        return $this->_recordCount;
    }

    public function setRecordCount($value){
        $this->_recordCount = $value;
        return $this;
    }

    public function getPageRange(){
        return $this->_pageRange;
    }

    public function setPageRange($value){
        $this->_pageRange = $value;
        return $this;
    }

    public function getPageSizes(){
        return $this->_pageSizes;
    }

    public function setPageSizes($value){
        $this->_pageSizes = $value;
        return $this;
    }

    public function getFirstPageText(){
        return $this->_firstPageText;
    }

    public function setFirstPageText($value){
        $this->_firstPageText = $value;
        return $this;
    }

    public function getLastPageText(){
        return $this->_lastPageText;
    }

    public function setLastPageText($value){
        $this->_lastPageText = $value;
        return $this;
    }

    public function getNextPageText(){
        return $this->_nextPageText;
    }

    public function setNextPageText($value){
        $this->_nextPageText = $value;
        return $this;
    }

    public function getPrevPageText(){
        return $this->_prevPageText;
    }

    public function setPrevPageText($value){
        $this->_prevPageText = $value;
        return $this;
    }

    public function getInformationTextFormat(){
        return $this->_informationTextFormat;
    }

    public function setInformationTextFormat($value){
        $this->_informationTextFormat = $value;
        return $this;
    }

    public function getPageSizeText(){
        return $this->_pageSizeText;
    }

    public function setPageSizeText($value){
        $this->_pageSizeText = $value;
        return $this;
    }

    public function getPageIndexTextFormat(){
        return $this->_pageIndexTextFormat;
    }

    public function setPageIndexTextFormat($value){
        $this->_pageIndexTextFormat = $value;
        return $this;
    }

    public function getPageSizeTextFormat(){
        return $this->_pageSizeTextFormat;
    }

    public function setPageSizeTextFormat($value){
        $this->_pageSizeTextFormat = $value;
        return $this;
    }

    public function getPageCount(){
        if(is_null($this->_pageCount)){
            $this->_pageCount = ceil($this->getRecordCount() / $this->getPageSize());
        }
        return $this->_pageCount;
    }

    public function getStartPage(){
        return $this->_startPage;
    }

    public function getEndPage(){
        return $this->_endPage;
    }

    public function isCurrentIndex($index){
        return $this->getPageIndex() == $index;
    }

    public function isCurrentSize($size){
        return $this->getPageSize() == $size;
    }

    public function isFirstPage(){
        return $this->getPageIndex() == 1;
    }

    public function isLastPage(){
        return $this->getPageIndex() == $this->getPageCount();
    }

    public function getPageIndexUrl($index){
        return Kd_Util_String::format($this->getUrlFormat(), array('index'=>$index, 'size'=>$this->getPageSize()));
    }

    public function getPageSizeUrl($size){
        return Kd_Util_String::format($this->getUrlFormat(), array('index'=>1, 'size'=>$size));
    }

    public function getInformationText(){
        return Kd_Util_String::format($this->getInformationTextFormat(), array('recordCount'=>$this->getRecordCount(), 'pageCount'=>$this->getPageCount(), 'pageIndex'=>$this->getPageIndex()));
    }

    public function getPageIndexText($index){
        if($this->getPageIndexTextFormat()){
            return sprintf($this->getPageIndexTextFormat(), $index);
        }
        return $index;
    }

    public function calculatePageNumber(){
        if($this->getPageIndex() < $this->getPageRange()){
            $this->_startPage = 1;
        }else{
            $ri = floor($this->getPageIndex() / $this->getPageRange());
            $this->_startPage = $ri * $this->getPageRange() - 1;
        }

        $this->_endPage = $this->_startPage + $this->getPageRange() + 1;
        if($this->_endPage > $this->getPageCount()){
            $this->_endPage = $this->getPageCount();
        }
        
        return $this;
    }

    protected function render(){
        return '';
    }
}